﻿using System;

namespace HomeWork3
{
    class MyException : Exception
    {
        public MyException(string message) : base(message)
        {
        }
    }
    interface Shape
    {
        // 接口成员

        Double CalculateArea();

    }
    class Square : Shape
    {
        private Double length;
        public Square(Double length)
        {
            if (length <= 0 )
            {
                throw new MyException("正方形各边必须大于0！");
            }
            else
            {
                this.length = length;
       
            }
        }



        public Double CalculateArea()
        {

            Console.WriteLine("此正方形面积为：{0}", length*length);
            return length * length;
        }
    }
    class Rectangle : Shape
    {
        private Double weight;
        private Double height;
        public Rectangle(Double weight, Double height)
        {
            if(weight<=0 || height <= 0)
            {
                throw new MyException("长方形各边必须大于0！");
            }
            else
            {
                this.weight = weight;
                this.height = height;
            }


        }


        public Double CalculateArea()
        {

            Console.WriteLine("此正方形面积为：{0}", weight * height);
            return weight * height;
        }
    }
    class Triangle : Shape
    {
        private Double side1;
        private Double side2;
        private Double side3;

        public Triangle(Double side1, Double side2, Double side3)
        {

            if (side1 <= 0 || side2 <= 0 || side3 <= 0)
            {
                throw new MyException("三角形各边必须大于0！");
            }
            else if((side1+side2)<=side3 || (side1 + side3) <= side2 || (side3 + side2) <= side1)
            {
                throw new MyException("三角形任意两边之和必须大于第三边！");
            }
            else
            {
                this.side1 = side1;
                this.side2 = side2;
                this.side3 = side3;
            }

        }
        public Double CalculateArea()
        {

            Double p = (side1 + side2 + side3) / 2;
            Double S = System.Math.Sqrt(p*(p - side1)*(p - side2)*(p - side3));

            Console.WriteLine("此三角形面积为：{0}", S);
            return S;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Square x1 = new Square(3);
            Square x2 = new Square(3.8);
            Rectangle x3 = new Rectangle(3,4);
            Square x4 = new Square(45);
            Square x5 = new Square(2.1);
            Triangle x6 = new Triangle(3,4,5);
            Square x7 = new Square(2.1);
            Square x8 = new Square(2.1);
            Triangle x9 = new Triangle(2.1,8.8,9.5);
            Rectangle x10 = new Rectangle(2,9);
            Double s = x1.CalculateArea()+ x2.CalculateArea()+ x3.CalculateArea()
                + x4.CalculateArea()+ x5.CalculateArea()+ x6.CalculateArea()+
                x7.CalculateArea()+ x8.CalculateArea()+ x9.CalculateArea()+ x10.CalculateArea();
            Console.WriteLine("这10个形状的面积为：{0}", s);
        }
    }
}
